/**
 * Created by john on 2018/11/8.
 */
var VALID = require("../config/valid");
var request = require("request");
//用于md5加密
var bcrypt = require('bcryptjs')
//加盐数
var SALT_WORK_FACTOR = 10
var comm = {};
var logger = require("../cors/log").getLogger("comm.js");
//用于退出登录
comm.loginOut = function (apps,curr) {
    for(var i in apps){
        if(i != curr){
            var sessionId = apps[i];
            var url =  VALID[i];
            if(url){
                var host = url.host + '/sso/loginout';
                requestLoginOut(host,sessionId);
            }
        }
    }
}

function requestLoginOut(url,sessionId) {
    request({
        timeout:5000,    // 设置超时
        method:'POST',    //请求方式
        url:url, //url
        form:{
            sessionid:sessionId
        }
    },function (error, response, body) {
        if (!error && response.statusCode == 200) {
            logger.debug("退出登录成功");
        }else{
            logger.info(url,sessionId);
            logger.info("退出登录失败",error);
        }
    });
}


comm.getHash = function (pwd) {
    return new Promise(function (resolve,reject) {
        bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
            if (err)
            {
                reject(err);
                return;
            }
            bcrypt.hash(pwd, salt, function (err, hash) {
                if (err){
                    reject(err);
                    return;
                }
                resolve(hash);
            })
        })
    })
}


module.exports=comm;
